package cn.yeyiu.sexyiu.adapter;

import android.content.Context;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.yeyiu.sexyiu.R;
import cn.yeyiu.util.AndroidUtil;
import cn.yeyiu.util.MediaUtil;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.VideoInfo;

public class VideoGuessAdapter extends RecyclerView.Adapter<VideoGuessAdapter.VerticalViewHolder> {
    public List<VideoInfo> guessVideoList = new ArrayList<>();
    private final Context mContext;
    private final LayoutInflater mInflater;

    public VideoGuessAdapter(Context context) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public VerticalViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = this.mInflater.inflate(R.layout.item_guess_video, viewGroup, false);
        DisplayMetrics displayMetrics = this.mContext.getResources().getDisplayMetrics();
        if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
            ((VideoView) inflate.findViewById(R.id.videoView)).setLayoutParams(new LinearLayout.LayoutParams(-2, -1));
        }
        return new VerticalViewHolder(inflate);
    }

    public void onBindViewHolder(VerticalViewHolder verticalViewHolder, int i) {
        VideoInfo videoInfo = this.guessVideoList.get(i);
        MediaUtil.fixLocalVideoInfo(videoInfo);

        VideoView videoView = verticalViewHolder.videoView;
        verticalViewHolder.videoTitleTv.setText(videoInfo.title);
        videoView.setVideoPath(videoInfo.videoSrc);

        verticalViewHolder.videoLLayout.setOnClickListener(v->{
            if (videoView.isPlaying()) {
                videoView.pause();
                verticalViewHolder.playIv.setVisibility(View.VISIBLE);
                return;
            }
            videoView.start();
            verticalViewHolder.playIv.setVisibility(View.INVISIBLE);
        });

        videoView.setOnPreparedListener(v->{
            videoView.start();
            verticalViewHolder.playIv.setVisibility(View.INVISIBLE);
        });
        verticalViewHolder.likeIv.setOnClickListener(v->{
            AndroidUtil.saveLikes(this.mContext, videoInfo);
        });
        verticalViewHolder.downloadIv.setOnClickListener(v->{
            NetUtil.downloadVideo(this.mContext, videoInfo);
        });
        verticalViewHolder.shareIv.setOnClickListener(v->{
            AndroidUtil.openInBrowser(this.mContext, Uri.parse(videoInfo.videoSrc));
        });
    }


    @Override // androidx.recyclerview.widget.RecyclerView.Adapter
    public int getItemCount() {
        return this.guessVideoList.size();
    }

    public static class VerticalViewHolder extends RecyclerView.ViewHolder {
        public ImageView downloadIv;
        public ImageView likeIv;
        public ImageView playIv;
        public ImageView shareIv;
        public LinearLayout videoLLayout;
        public TextView videoTitleTv;
        public VideoView videoView;

        public VerticalViewHolder(View view) {
            super(view);
            this.videoTitleTv = (TextView) view.findViewById(R.id.data_title_tv);
            this.videoView = (VideoView) view.findViewById(R.id.videoView);
            this.playIv = (ImageView) view.findViewById(R.id.video_play_iv);
            this.videoLLayout = (LinearLayout) view.findViewById(R.id.video_view_ll);
            this.shareIv = (ImageView) view.findViewById(R.id.share_btn);
            this.likeIv = (ImageView) view.findViewById(R.id.like);
            this.downloadIv = (ImageView) view.findViewById(R.id.download);
        }
    }
}
